package com.pn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pn.entity.Brand;
import com.pn.entity.Unit;
import com.pn.mapper.BrandMapper;
import com.pn.mapper.UnitMapper;
import com.pn.service.BrandService;
import com.pn.service.UnitService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = "com.pn.service.impl.UnitServiceImpl")
@Service
public class UnitServiceImpl implements UnitService {

    @Autowired
    private UnitMapper unitMapper;

    //显示单位信息
    @Override
    public PageBean unitPageList(Unit unit) {
        PageHelper.startPage(unit.getPageNum(),unit.getPageSize());
        Page<Unit> page = unitMapper.unitPageList(unit);
        return new PageBean(page.getTotal(),page.getResult());
    }

    //修改单位
    @CacheEvict(key = "'all:unit'")
    @Override
    public Result unitUpdate(Unit unit) {

        int i = unitMapper.unitUpdate(unit);
        if (i > 0 ){
            return Result.ok("修改单位成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"修改单位失败");
    }

    //添加单位
    @CacheEvict(key = "'all:unit'")
    @Override
    public Result unitAdd(Unit unit) {

        int i = unitMapper.unitInsert(unit);
        if (i > 0 ){
            return Result.ok("添加单位成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"添加单位失败");
    }

    //删除单位
    @CacheEvict(key = "'all:unit'")
    @Override
    public Result unitDelete(Integer unitId) {

        int i = unitMapper.unitDelete(unitId);
        if (i > 0 ){
            return Result.ok("删除单位成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"删除单位失败");
    }

    //显示单位
    @Cacheable(key = "'all:unit'")
    @Override
    public List<Unit> unitList() {
        List<Unit> unitList =  unitMapper.unitList();
        return unitList;
    }
}
